package com.yunhai.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.FmsFormulaRuleEntity;
import com.yunhai.model.entity.fms.FmsFormulaRuleVo;
import com.yunhai.model.vo.fms.FmsFormulaRuleQuery;
import com.yunhai.recipe.mapper.FormulaRuleMapper;
import com.yunhai.recipe.service.FormulaRuleService;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FormulaRuleServiceImpl implements FormulaRuleService {

    @Resource
    private FormulaRuleMapper formulaRuleMapper;

    /**
     * 获取 FormulaRule 分页列表
     */
    @Transactional
    public IPage getFormulaRulePagination(PaginationVo paginationVo, FmsFormulaRuleQuery fmsFormulaRuleQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FmsFormulaRuleEntity> lqs = new LambdaQueryWrapper();
        formulaRuleMapper.selectPage(page, lqs);

        List<FmsFormulaRuleEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FormulaRule 所有数据
     *
     */
    @Transactional
    public List<FmsFormulaRuleEntity> getFormulaRuleAllList( FmsFormulaRuleQuery fmsFormulaRuleQuery ) throws Exception {

        LambdaQueryWrapper<FmsFormulaRuleEntity> lqs = new LambdaQueryWrapper();
        List<FmsFormulaRuleEntity> list = formulaRuleMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FormulaRule 详情
     */
    @Transactional
    public FmsFormulaRuleEntity getFormulaRuleDetail( Long id) throws Exception {

        FmsFormulaRuleEntity fmsFormulaRuleEntity = formulaRuleMapper.selectById(id);
        return fmsFormulaRuleEntity;
    }


    /**
     * 新增 FormulaRule
     * */
    @Transactional
    public void addFormulaRule(FmsFormulaRuleVo fmsFormulaRuleVo) throws Exception {

        FmsFormulaRuleEntity fmsFormulaRuleEntity = new FmsFormulaRuleEntity();
        ReflectionCopy.copyProperties( fmsFormulaRuleEntity, fmsFormulaRuleVo );
        formulaRuleMapper.insert(fmsFormulaRuleEntity);
        return;
    }


    /**
     * 更新 FormulaRule
     * */
    @Transactional
    public void updateFormulaRule( FmsFormulaRuleVo fmsFormulaRuleVo) throws Exception {

        if( fmsFormulaRuleVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FmsFormulaRuleEntity fmsFormulaRuleEntity = new FmsFormulaRuleEntity();
        ReflectionCopy.copyProperties( fmsFormulaRuleEntity, fmsFormulaRuleVo );
        formulaRuleMapper.updateById(fmsFormulaRuleEntity);
        return;
    }

    /**
     * 删除 FormulaRule
     * */
    @Transactional
    public void batchRemoveFormulaRule( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        formulaRuleMapper.deleteBatchIds(ids);
    }

}